package com.engine.kq.cmd.hrmAttProcSet;

import com.api.browser.bean.SearchConditionItem;
import com.api.browser.bean.SearchConditionOption;
import com.api.hrm.bean.HrmFieldBean;
import com.api.hrm.util.HrmFieldSearchConditionComInfo;
import com.api.hrm.util.HrmFieldUtil;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import com.engine.kq.wfset.attendance.domain.HrmAttProcAction;
import com.engine.kq.wfset.attendance.domain.HrmAttProcSet;
import com.engine.kq.wfset.attendance.manager.HrmAttProcActionManager;
import com.engine.kq.wfset.attendance.manager.HrmAttProcSetManager;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import weaver.common.StringUtil;
import weaver.conn.RecordSet;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;

/**
 * 获取考勤流程设置 动作设置
 * @author pzy
 *
 */
public class GetStateProcSetFlowWfSetCmd extends AbstractCommonCommand<Map<String, Object>>{

	public GetStateProcSetFlowWfSetCmd(Map<String, Object> params, User user) {
		this.user = user;
		this.params = params;
	}

	@Override
	public BizLogContext getLogContext() {
		return null;
	}


	@Override
	public Map<String, Object> execute(CommandContext commandContext) {
		Map<String, Object> retmap = new HashMap<String, Object>();

		List<Object> lsGroup = new ArrayList<Object>();
		Map<String, Object> groupitem = null;
		Map<String, Object> groupitems = null;
		List<Object> itemlist = null;
		try {

			StringUtil strUtil = new StringUtil();
			RecordSet rs = new RecordSet();
			HrmAttProcSetManager attProcSetManager = new HrmAttProcSetManager();
			HrmAttProcActionManager attProcActionManager = new HrmAttProcActionManager();
			itemlist = new ArrayList<Object>();
			groupitem = new HashMap<String, Object>();

			String id = StringUtil.vString(params.get("id"));
			HrmAttProcSet sBean = attProcSetManager.get(id);
			attProcActionManager.setUser(user);
			String wfId = sBean.getField001() + "";
			int flowtype = sBean.getField006();//考勤流程类型
			String splitAction = KqSplitActionEnum.ATT.getSplitAction();
			List list = attProcActionManager.find("[map]field001:" + id + ";field004:0;wfId:" + sBean.getField001());
			if (list == null || list.size() == 0) {
				list = attProcActionManager.find("[map]field001:-1;field004:1;mfid:" + flowtype);
			}

			//生成字段的集合
			Map<String, Object> mapFields = getFieldsInfo(list, wfId, splitAction, flowtype);
			groupitem.put("table", mapFields);
			itemlist.add(groupitem);
			retmap.put("title", SystemEnv.getHtmlLabelName(1361, user.getLanguage()));
			retmap.put("defaultshow", true);
			retmap.put("items", itemlist);

			retmap.put("status", "1");
		} catch (Exception e) {
			retmap.put("status", "-1");
			retmap.put("message", SystemEnv.getHtmlLabelName(382661, user.getLanguage()));
			writeLog(e);
		}

		return retmap;
	}
	
	/**
	 * 生成字段对应的集合数据
   * @param list
   * @param wfId
   * @param splitAction
   * @param flowtype
   */
	private Map<String,Object> getFieldsInfo(List list, String wfId, String splitAction, int flowtype){

		Map<String,Object> infoMap = new HashMap<String, Object>();
		List<Map<String,Object>> datasList = new ArrayList<Map<String,Object>>();
		List<Map<String,Object>> browserList = new ArrayList<Map<String,Object>>();
		Map<String,Object> browserMap = new HashMap<String, Object>();
		Map<String,Object> data = new HashMap<String, Object>();
		
		List<Map<String,Object>> columnsList = new ArrayList<Map<String,Object>>();
		
		Map<String,Object> allMap = setColumns(wfId);
		if(allMap != null){
			columnsList = (List<Map<String, Object>>) allMap.get("column");
			browserList = (List<Map<String, Object>>) allMap.get("browser");
		}
    HrmAttProcAction bean = null;
		List<Object> lFields =  new ArrayList<Object>();
		String[] fields = new String[4];
		RecordSet rs = new RecordSet();
		String tmpField006Name = "",tmpField006 = "";
		for(int i=0; i<(list==null?0:list.size());i++) {
			bean = (HrmAttProcAction)list.get(i);

			List<SearchConditionOption> options = new ArrayList<SearchConditionOption>();
			lFields = new ArrayList<Object>();
			
			options.add(new SearchConditionOption("2",SystemEnv.getHtmlLabelName(18009,user.getLanguage()),(bean.getField005() == 2 ? true : false)));
			options.add(new SearchConditionOption("1",SystemEnv.getHtmlLabelName(18010,user.getLanguage()),(bean.getField005() == 1 ? true : false)));
			options.add(new SearchConditionOption("0",SystemEnv.getHtmlLabelName(386027,weaver.general.Util.getIntValue(user.getLanguage())),(bean.getField005() == 0 ? true : false)));
			String browserVal = bean.getField006().equals("-1") || bean.getField006().equals("0") ? "" : String.valueOf(bean.getField006());
			String checkBoxVal = bean.getField007() == 1 ? "1" : "0";
			String field005Val = bean.getField005() < 0 ? "2" : ""+bean.getField005();

			String key = bean.getField002();
			String td0 = "fakeInp";
			String td1 = "field003";
			String td2 = "field005";
			String td3 = "field006";
			String td3Span = "field006span";
//			String td4 = "field007";
			String td5 = "field008";

			data = new HashMap<String, Object>();
			data.put("rowKey", key);
			if(flowtype == 5){
        data.put(td0, SystemEnv.getHtmlLabelName(33604,weaver.general.Util.getIntValue(user.getLanguage())));
      }else {
        data.put(td0, SystemEnv.getHtmlLabelNames(bean.getField003(),user.getLanguage()));
      }
			data.put(td1, bean.getField003());
			data.put(td2, ""+field005Val);
			data.put(td3, browserVal);
			data.put(td3Span, bean.getField006Name());
//			data.put(td4, checkBoxVal);
			data.put(td5, ""+bean.getField008());
      if(splitAction.equalsIgnoreCase(bean.getField002())){
        if(flowtype != 5){
          data.put("fakeInpHelpful", SystemEnv.getHtmlLabelName(10000814,weaver.general.Util.getIntValue(user.getLanguage())));
        }
      }
      //TODO 未考虑做这个创建节点后拆分流程的功能，暂时屏蔽
//      if(splitFreezeAction.equalsIgnoreCase(bean.getField002())){
//        data.put("fakeInpHelpful", SystemEnv.getHtmlLabelNames("389468",user.getLanguage()));
//      }
			datasList.add(data);
		}
		infoMap.put("columns", columnsList);
		infoMap.put("datas", datasList);
		infoMap.put("browsers", browserList);
		return infoMap;
	}

	private Map<String,Object> setColumns(String wfId) {
		Map<String,Object> allMap = new HashMap<String, Object>();
		List<HrmFieldBean> titles = null;
		List<HrmFieldBean> btitles = null;
		List<Map<String, Object>> columns = null;
		List<Map<String, Object>> bcolumns = null;
		String td0 = "fakeInp"+",19831,7,1";
		String td2 = "field005"+",33408,5,1";
		String td3 = "field006"+",33410,3,workflowNode";
		String td4 = "field007"+",33409,4,1";
		
		String btd1 = "field006"+",15587,3,wflink";
		String btd2 = "field006"+",15586,3,workflowNode";
		
		String[] fields = new String[]{td0,td2,td3,btd1,btd2};
		HrmFieldSearchConditionComInfo hrmFieldSearchConditionComInfo = new HrmFieldSearchConditionComInfo();
		SearchConditionItem searchConditionItem = null;
		HrmFieldBean hrmFieldBean = null;
		titles = new ArrayList<HrmFieldBean>();
		btitles = new ArrayList<HrmFieldBean>();
		List<SearchConditionOption> selectOption = null;
		Map<String, Object> otherparam = new HashMap<String, Object>();
		otherparam.put("workflowid", wfId);
		otherparam.put("isSingle", false);
		otherparam.put("noNeedActiveWfId", "1");
		for(int i=0;i<fields.length;i++){
			selectOption = new ArrayList<SearchConditionOption>();
			String[] fieldinfo = fields[i].split(",");
			hrmFieldBean = new HrmFieldBean();
			hrmFieldBean.setFieldname(fieldinfo[0]);
			hrmFieldBean.setFieldlabel(fieldinfo[1]);
			hrmFieldBean.setFieldhtmltype(fieldinfo[2]);
			hrmFieldBean.setType(fieldinfo[3]);
			
			if("field005".equals(fieldinfo[0])){
				selectOption.add(new SearchConditionOption("2",SystemEnv.getHtmlLabelName(18009, user.getLanguage()),true));
				selectOption.add(new SearchConditionOption("1",SystemEnv.getHtmlLabelName(18010, user.getLanguage())));
				selectOption.add(new SearchConditionOption("0",SystemEnv.getHtmlLabelName(386027,weaver.general.Util.getIntValue(user.getLanguage()))));
				hrmFieldBean.setSelectOption(selectOption);
			}
			if("workflowNode".equals(fieldinfo[3])){
				hrmFieldBean.setOtherparam(otherparam);
			}
			if("wflink".equals(fieldinfo[3])){
				hrmFieldBean.setOtherparam(otherparam);
			}
			if(i > 2){
				btitles.add(hrmFieldBean);
			}else{
				titles.add(hrmFieldBean);
			}
		}
		columns = HrmFieldUtil.getHrmDetailTable(titles, null, user);
		bcolumns = HrmFieldUtil.getHrmDetailTable(btitles, null, user);
		allMap.put("column", columns);
		allMap.put("browser", bcolumns);
		return allMap;
	}
	

}
